'use strict';
const dayjs = require('dayjs');
module.exports = app => {
    const { INTEGER, STRING,TEXT } = app.Sequelize;
    const Course = app.model.define(
      'Course',
          {
              id: {type: INTEGER, primaryKey: true, autoIncrement: true},
              category_id: INTEGER,
              name: STRING,
              description: STRING,
              xn_buynum: INTEGER,
              nj_name: STRING,
              num: INTEGER,
              teacher_name: STRING,
              post_url: STRING,
              content:TEXT,
              sort: INTEGER,
              status: INTEGER,
              created_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('created_time')).format('YYYY-MM-DD HH:mm:ss');
                }
              },
              updated_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
              }
          },
          {
              tableName: 'courses',
              timestamps: true, // 启用时间戳(createdAt, updatedAt)
              updatedAt: 'updated_time', // 不想要 updatedAt
              createdAt: 'created_time', // 想要  createdAt 但是希望名称叫做 created_at
          }
    );
    Course.associate = function() {
        app.model.Course.belongsTo(app.model.CourseCategories, {
            foreignKey: 'category_id',
            targetKey: 'id',
        });
    }
    return Course;
};
